Multiple Instances and data

  • Note

    We can create multiple instances of a class, each with its own set of data. Each instance has its own properties and methods, and they don't interfere with each other. Here's a simple example to illustrate this concept:

    
    class Car {
        public $brand;
        public $model;
        public $color;
    
        public function __construct($brand, $model, $color) {
            $this->brand = $brand;
            $this->model = $model;
            $this->color = $color;
        }
    
        public function displayInfo() {
            echo "Brand: {$this->brand}, Model: {$this->model}, Color: {$this->color}\n";
        }
    }
    
    // Creating multiple instances of the Car class
    $car1 = new Car("Toyota", "Camry", "Blue");
    $car2 = new Car("Honda", "Accord", "Red");
    
    // Displaying information about each car
    $car1->displayInfo();  // Outputs: Brand: Toyota, Model: Camry, Color: Blue
    $car2->displayInfo();  // Outputs: Brand: Honda, Model: Accord, Color: Red